﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;

using SolrExample.Tests;
using NUnit.Framework;
using Spring.Context;
using Spring.Context.Support;
using SolrExample.Common;
using SolrExample.Interface.DomainObject;
using SolrNet.Impl;
using SolrNet.Commands;
using SolrExample.Tests.SolrClient;
using System.Collections.Specialized;
using SolrNet;

namespace SolrExample.Temp.Tests
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestFixture]
    public class Solr
    {
        private static SearchEngineCommand mSearchEngine = new SearchEngineCommand();
        [TestFixtureSetUp]
        public void InitializeAllTests()
        {

        }

        [Test]
        public void Test1()
        {
            IApplicationContext applicationContext = ContextRegistry.GetContext();
            JobServices jobServices = applicationContext.GetObject("JobServices") as JobServices;
            jobServices.CommitIndex();

        }

        [Test]
        public void Test2() {
            IList<IPerson> listPerson = new List<IPerson>();
            for (var i = 1; i <= 6; i++)
            {
                IPerson person = new Person() {
                    Id = 1000 + i,
                    CountryCode = "HK",
                    LastModifiedDate = DateTime.Now,
                    Name = "Miragelu" + i,
                    addtion = "addtion"
                };
                listPerson.Add(person);
            }
            string hardServices = "HB#http://localhost:8080/solr-HDM";
            string jobsDBTextFieldsStr = "";

            Dictionary<string, Dictionary<string, bool>> postedResult = CreateIndexFromHardCard(listPerson, hardServices, jobsDBTextFieldsStr);
            bool success = null != postedResult.Values.SingleOrDefault(i => i.Values.Contains(true));
            Console.WriteLine("Post result: " + success);

            try
            {
                string searchKey = "name:miragelu1";
                Dictionary<string, bool> deletedResult = DeleteCacheFromSearchKey(searchKey, "HK", hardServices);
                //bool deleted = false != deletedResult.Values.SingleOrDefault(i => i==true);
                //Console.WriteLine("Delte result: " + deleted);
            }
            catch { }

            try { 
                IList<string> Ids = new List<string>(){"10003", "1005"};
                Dictionary<string, bool> deletedIdsResult = DeleteHarddiskFromIds(Ids, "HK", hardServices);
                Console.WriteLine("");
            }
            catch { }
                        
        }


        [Test]
        public void Test3()
        {
            ISolrConnection con = new SolrConnection("http://localhost:8088/solr");
            NameValueCollection options = new NameValueCollection();




        }

        [TestFixtureTearDown]
        public void CleanupAllTests() { }

        /// <summary>
        /// Create Index To Hard Card
        /// </summary>
        /// <param name="jobAds"></param>
        /// <returns></returns>
        public static Dictionary<string, Dictionary<string, bool>> CreateIndexFromHardCard(IList<IPerson> person, string persistantServers, string jobsDBTextFields)
        {
            //step 1: get record index xml.
            Dictionary<string, string> indexFieldXmls = IndexFieldFactory.ComposeIndexFieldXml(person, true, jobsDBTextFields);

            //step 2: post index xml to search engine
            Dictionary<string, Dictionary<string, bool>> postedResult = new Dictionary<string, Dictionary<string, bool>>();
            List<string> searchEngineUrls = persistantServers.Split(';').ToList();
            foreach (string key in indexFieldXmls.Keys)
            {
                //string jobsDBCountryCode = CommonConfig.GetConfigValue("JobsDB_" + key);
                Dictionary<string, bool> methodResults = mSearchEngine.PostXml(searchEngineUrls, indexFieldXmls[key], key, true);
                postedResult[key] = methodResults;
            }
            return postedResult;
        }

        public static Dictionary<string, bool> DeleteCacheFromSearchKey(string searchKey, string jobsDbCountryCode, string cacheServers)
        {
            List<string> searchEngineUrls = cacheServers.Split(';').ToList();
            return mSearchEngine.DeleteSearchKey(searchEngineUrls, searchKey, jobsDbCountryCode, true);
        }

        public static Dictionary<string, bool> DeleteHarddiskFromIds(IList<string> recoreId, string jobsDbCountryCode, string servers)
        {
            Dictionary<string, bool> dicResult = new Dictionary<string, bool>();
            List<string> searchEngineUrls = servers.Split(';').ToList();
            dicResult = mSearchEngine.Delete(searchEngineUrls, recoreId, jobsDbCountryCode, true);
            return dicResult;
        }
    }


    public interface IPerson : IIndexRecord
    {
        string Name { get; set; }
        string addtion { get; set; }
    }
    public class Person : IPerson, IIndexRecord {
        public long Id { get; set; }
        public string CountryCode { get; set; }
        public DateTime LastModifiedDate { get; set; }
        public bool IsSearchableRecord { get { return true; } }

        public string Name { get; set; }
        public string addtion { get; set; }
    }

}
